package com.ruis.kotboot.common.filter

import com.ruis.kotboot.common.util.ruisUtil
import org.apache.commons.lang3.StringUtils
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletRequestWrapper

class RuisXssWrapper(val req: HttpServletRequest): HttpServletRequestWrapper(req) {

    override fun getHeader(name: String?): String? {
        var value=super.getHeader(name)
        val skip=req.getAttribute("xss-skip")
        if(skip=="1")return value
        if (StringUtils.isNotBlank(value))
            value=ruisUtil.clearHtmls(value,false)
        return value
    }

    override fun getParameter(name: String?): String? {
        var value=super.getParameter(name)
        val skip=req.getAttribute("xss-skip")
        if(skip=="1")return value
        if (StringUtils.isNotBlank(value))
            value=ruisUtil.clearHtmls(value,false)
        return value
    }

    override fun getParameterValues(name: String?): Array<String>? {
        val values=super.getParameterValues(name)
        val skip=req.getAttribute("xss-skip")
        if(skip=="1")return values
        if (values!=null&&values.isNotEmpty()){
            for (i in 0 until values.size)
                values[i]=ruisUtil.clearHtmls(values[i],false)
        }
        return values
    }
}